home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 September / Software of the Month Club 1996 September.iso / mac / Desktop Publishing / PageSpinner 1.1 ƒ / JavaScript Examples / Java Random Link Stationery < prev    next >
Encoding:
Text File  |  1996-05-20  |  3.5 KB  |  130 lines  |  [TEXT/JyWs]

  1. <HTML><HEAD>
  2. <TITLE>JavaScript Random Link</TITLE>
  3.  
  4. <SCRIPT LANGUAGE="JavaScript">
  5. <!-- Beginning of JavaScript --------
  6. /* 
  7.         GetRandomURL
  8.         Written by Jerry ┼man, Optima System, May 19, 1996.
  9.  
  10.         We will not be held responsible for any unwanted 
  11.         effects due to the usage of this script or any derivative.  
  12.         No warrantees for usability for any specific application are given
  13.         or implied.
  14. */
  15.  
  16. function GetRandomURL()
  17. {
  18.     // Put relative or full URL's in the strings below
  19.     // You can increase the number of URL's to more than 5 by adding a
  20.     // string containing an URL in list below
  21.  
  22. var locationlist = new URLList (
  23.         "harpo.html",
  24.         "groucho.html",
  25.         "chico.html",
  26.         "zeppo.html",
  27.         "Java Scrolling Text Stationery"
  28.         );
  29.  
  30.     num = Math.round ( ( rand.next() * (locationlist.count-1)) );
  31.  
  32.  return locationlist.list[num];
  33. }
  34.  
  35. function URLList () {
  36.   var argv = URLList.arguments;
  37.   var argc = argv.length;
  38.   this.list = new Object();
  39.   for (var i = 0; i < argc; i++)
  40.     this.list[i] = argv[i];
  41.   this.count = argc;
  42.   return this;
  43. }
  44.  
  45. //*********************************************
  46. // Park-Miller Pseudo-Random Number Generator
  47. // JavaScript implementation by David N. Smith
  48. // of IBM's T J Watson Research Center
  49. //*********************************************
  50. function NextRandomNumber()  {
  51.   var hi   = this.seed / this.Q;
  52.   var lo   = this.seed % this.Q;
  53.   var test = this.A * lo - this.R * hi;
  54.   if (test > 0)
  55.     this.seed = test;
  56.   else
  57.     this.seed = test + this.M;
  58.   return (this.seed * this.oneOverM);
  59. }
  60. function RandomNumberGenerator() {
  61.   var d = new Date();
  62.   this.seed = 2345678901 +
  63.     (d.getSeconds() * 0xFFFFFF) +
  64.     (d.getMinutes() * 0xFFFF);
  65.   this.A = 48271;
  66.   this.M = 2147483647;
  67.   this.Q = this.M / this.A;
  68.   this.R = this.M % this.A;
  69.   this.oneOverM = 1.0 / this.M;
  70.   this.next = NextRandomNumber;
  71.   return this;
  72. }
  73.  
  74. var rand = new RandomNumberGenerator();
  75.  
  76. // -- End of JavaScript code -------------- -->
  77. </SCRIPT>
  78.  
  79. </HEAD>
  80. <BODY>
  81. <H1>JavaScript Random Link</H1>
  82.  
  83.  
  84. <B>This stationery page contains:</B>
  85. <BR>
  86. <B><UL>
  87. <LI>a JavaScript that selects a random URL
  88. <LI>a link that displays an alert
  89. </UL>
  90. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher.
  91. </B>
  92.  
  93. <P>
  94. The script is named <B>GetRandomURL</B> and it is placed in the HEAD section of the HTML document. The script is executed by clicking on a link containing a call to GetRandomURL(). Also note the <FONT COLOR="FF3366">custom text</FONT> in Netscapes status area.
  95. <P>
  96.  
  97. Example of this script picking a 
  98. <A HREF=""
  99.  onClick="this.href=GetRandomURL()"
  100.  onMouseOver="window.status='This link goes to a randomly selected page'; return true">random</A> page.
  101. <P>
  102. <B>How to use:</B><BR>
  103. Replace the filenames inside the script with your own URLs and edit this page contents inside the <BODY> section. (You can also copy the entire script to an existing page).
  104.  
  105. <XMP>function GetRandomURL()
  106. {
  107. var locationlist = new URLList (
  108.     "harpo.html",
  109.     "groucho.html",
  110.     "chico.html",
  111.     "zeppo.html",
  112.     "http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/"
  113.     );
  114. </XMP>
  115. <P>
  116. Use code similar to this to let the user execute the script:
  117. <XMP><A HREF=""
  118.  onClick="this.href=GetRandomURL()"
  119.  onMouseOver="window.status='This link takes you to some unknown place';
  120.  return true">Go</A> </XMP>
  121. <HR>
  122.  
  123. <H2>Alert Dialog Example</H2>
  124. <A HREF="#"
  125.  onClick="window.alert('This is a special message to you!');"
  126.  onMouseOver="window.status='This is not a regular link'; return true">Example</A> of a link displaying an alert dialog.
  127.  
  128. </BODY>
  129. </HTML>
  130.